import java.util.Stack;

public class Solution20 {
    public boolean isValid(String s) {
        int a=0,b=0,c=0;
        Stack<Integer> queue=new Stack<>();
        for (char ch : s.toCharArray()) {
            if (ch=='('){
                a++;
                queue.push(1);
            }else if (ch=='['){
                b++;
                queue.push(2);
            }else if (ch=='{'){
                c++;
                queue.push(3);
            }else if (ch==')'){
                if (a==0 || queue.isEmpty() || queue.pop()!=1){
                    return false;
                }
                a--;
            }else if (ch==']'){
                if (b==0 || queue.isEmpty() || queue.pop()!=2){
                    return false;
                }
                b--;
            }else{
                if (c==0 || queue.isEmpty() || queue.pop()!=3){
                    return false;
                }
                c--;
            }
        }
        return queue.isEmpty() && a==0 && b==0 && c==0;
    }

    public static void main(String[] args) {
        System.out.println(new Solution20().isValid("([)]"));
    }
}
